﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Xml.Serialization;
using System.Xml.Linq;

namespace CotizacionPresencialPaquetes.Models
{
    public class MenuConfiguration
    {        
        public static List<ItemMenu> Read()
        {
            MenuConfiguration result = new MenuConfiguration();

            XElement xmlMenu = XElement.Load(System.Web.HttpContext.Current.Server.MapPath("~/") + "XMLMenuConfiguration.xml");
            var itemMenuAll = (from m in xmlMenu.Descendants("ItemMenu")
                               where m.Parent.Name.ToString() == "MenuConfiguration"
                               select new ItemMenu { 
                                   DisplayText = m.Element("DisplayText").Value,
                                   ControllerName = m.Element("ControllerName").Value,
                                   ActionName = m.Element("ActionName").Value,
                                   Order = int.Parse(m.Element("Order").Value),
                                   MenuType = (m.Element("MenuType").Value == "CMS" ? MenuTypeEnum.CMS : MenuTypeEnum.PATH),
                                   ItemName = m.Element("ItemName").Value,
                                   PathFile = (m.Element("PathFile") != null ?m.Element("PathFile").Value: ""),
                                   SubMenu = (from s in m.Elements("SubMenus").Descendants("ItemMenu")
                                              select new ItemMenu { 
                                               DisplayText = s.Element("DisplayText").Value,
                                               ControllerName = s.Element("ControllerName").Value,
                                               ActionName = s.Element("ActionName").Value,
                                               Order = int.Parse(s.Element("Order").Value),
                                               MenuType = (s.Element("MenuType").Value == "CMS" ? MenuTypeEnum.CMS : MenuTypeEnum.PATH),
                                               ItemName = s.Element("ItemName").Value,
                                               PathFile = (s.Element("PathFile") != null ?s.Element("PathFile").Value: "")
                                              }).ToList()
                               }).ToList();
            return itemMenuAll;
        } 

    }
}